package cn.tedu.baidashi.controller;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

//根据当前登录用户的角色,跳转不同首页
@Controller
public class HomeController {
    //将角色定义为常量
    public static final GrantedAuthority ADMIN = new SimpleGrantedAuthority("ROLE_ADMIN");
    public static final GrantedAuthority GUEST = new SimpleGrantedAuthority("ROLE_GUEST");

    //将登录判断路径设置为 以下 "/","/guest_index.html" 两个路径
    @GetMapping(value = {"/","/guest_index.html"})
    public String index(
            @AuthenticationPrincipal UserDetails user
            ){
        if(user.getAuthorities().contains(ADMIN)){
            return "redirect:/back/index.html";
        }else if(user.getAuthorities().contains(GUEST)){
            return "redirect:/index.html";
        }
        return null;
    }

}
